class Solution {
public:
    stack<int>s1;
    stack<int>s2;
    int get(string &s, int l, int r){
        int sum = 0;
        for(int i = l; i <= r; i ++){
            sum = 10 * sum + (s[i] - '0');
        }
        return sum;
    }
    int calculate(string s) {
        int n = s.size();
        for(int i = 0; i < n; i ++){
            if(s[i]==' ')continue;
            else if(s[i]>='0'&&s[i]<='9'){
                int j = i + 1;
                while(j < n && (s[j]>='0'&&s[j]<='9')) j++;
                int val = get(s,i,j-1);
                if(s2.size()&&s2.top()=='*') {s1.top() *= val; s2.pop();}
                else if(s2.size()&&s2.top() == '/') {s1.top() /= val; s2.pop();}
                else {
                    s1.push(val);
                }
                if(j == n) break;
                i = j - 1;
            }else {
                // 2 + 2 - 
                if(s2.size() && (s[i]=='+'||s[i]=='-')){
                   int val = s1.top(); s1.pop();
                   if(s2.top()=='+') s1.top() += val;
                   else s1.top() -= val;
                   s2.pop();
                   s2.push(s[i]); 
                }else s2.push(s[i]);
            }
        }
         if(s1.size()>1){
                 int val = s1.top(); s1.pop();
                 if(s2.top()=='+') s1.top() += val;
                 else s1.top() -= val;
                 s2.pop();
        } 
        return s1.top();
    }
};